package db;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import model.Enterprise;
import model.EnterprisePersonRelation;
import model.Person;

public class EnterprisePersonRelationDAO {
	private static final String TABLE = "enterprise_person_relation";	
	private static String onSqlError = "SQL statement fault";
		
	private static final String 
		ID_FIELD = "enterprise_person_relation",			
		PERSON_FIELD = "person_fk",
		ENTERPRISE_FIELD = "enterprise_fk",
		ENT_PER_RELATION_TYPE_FIELD = "ent_per_relation_type_fk";
	
	private static final String INSERT_STATEMENT_STRING = 
			"INSERT INTO "+TABLE+" ("+PERSON_FIELD+", "+ENTERPRISE_FIELD+", " +
					""+ENT_PER_RELATION_TYPE_FIELD+") VALUES (?, ?, ?)";
	
	private static final String UPDATE_STATEMENT_STRING = 
			"UPDATE "+TABLE+" SET " +PERSON_FIELD+" = ?, "+ENTERPRISE_FIELD+" = ?, " 
			+ENT_PER_RELATION_TYPE_FIELD+" = ? WHERE "+ID_FIELD+" = ?";
	
	private static final String DELETE_STATEMENT_STRING =
			"DELETE FROM "+TABLE+" WHERE "+ID_FIELD+" = ?";
	
	private static PreparedStatement insertStatement, updateStatement, deleteStatement;
	
	static{
		try {
			insertStatement = ImbiConnection.getConnection().prepareStatement(INSERT_STATEMENT_STRING);
			updateStatement = ImbiConnection.getConnection().prepareStatement(UPDATE_STATEMENT_STRING);
			deleteStatement = ImbiConnection.getConnection().prepareStatement(DELETE_STATEMENT_STRING);
		} catch (SQLException e) {			
			e.printStackTrace();
			System.exit(0);
		}
	}
	
	public EnterprisePersonRelation find(int id) {			
		try{				
			Statement st = ImbiConnection.getConnection().createStatement();
			st.executeQuery("SELECT * FROM "+TABLE+" WHERE "+ID_FIELD+" = "+id);
			EnterprisePersonRelation e = extractEnterprisePersonRelation(st.getResultSet()).get(0);			
			return e;
		} catch (SQLException | IndexOutOfBoundsException e){
				System.out.println(onSqlError);				
				return null;
		}		
	}

	private ArrayList<EnterprisePersonRelation> extractEnterprisePersonRelation(ResultSet r) throws SQLException {
		ArrayList<EnterprisePersonRelation> list = new ArrayList<EnterprisePersonRelation>();					
		while(r.next()){
			EnterprisePersonRelation e = new EnterprisePersonRelation(
				r.getInt(1),
				r.getInt(2),
				r.getInt(3),
				r.getInt(4)
			);
			list.add(e);
		}
		return list;
	}	
	
	public ArrayList<EnterprisePersonRelation> findAllByEnterprise(Enterprise e) {
		try{				
			Statement st = ImbiConnection.getConnection().createStatement();
			st.executeQuery("SELECT * FROM "+TABLE+" WHERE "+ENTERPRISE_FIELD+" = "+e.getId());								
			return extractEnterprisePersonRelation(st.getResultSet());			
		} catch (SQLException s){
				System.out.println(onSqlError);				
				return null;
		}	
	}	
	
	public ArrayList<EnterprisePersonRelation> findAllByPerson(Person person) {
		try{				
			Statement st = ImbiConnection.getConnection().createStatement();
			st.executeQuery("SELECT * FROM "+TABLE+" WHERE "+PERSON_FIELD+" = "+person.getId());								
			return extractEnterprisePersonRelation(st.getResultSet());			
		} catch (SQLException s){
				System.out.println(onSqlError);				
				return null;
		}
	}
	
	public void insert(EnterprisePersonRelation e){
		try{											
		    insertStatement.setInt(1, e.getPerson());
		    insertStatement.setInt(2, e.getEnterprise());
		    insertStatement.setInt(3, e.getEntPerRelationType());	
		    insertStatement.executeUpdate();			
		} catch (SQLException s){
				System.out.println(onSqlError);							
		}		
	}
	
	public void update(EnterprisePersonRelation e){
		try{										
			updateStatement.setInt(1, e.getPerson());
		    updateStatement.setInt(2, e.getEnterprise());
		    updateStatement.setInt(3, e.getEntPerRelationType());	
		    updateStatement.setInt(4, e.getId());
		    updateStatement.executeUpdate();			
		} catch (SQLException s){
				System.out.println(onSqlError);							
		}			
	}
	
	public void delete(EnterprisePersonRelation e){
		try{										
		    deleteStatement.setInt(1, e.getId());		    
		    deleteStatement.executeUpdate();			
		} catch (SQLException s){
				System.out.println(onSqlError);					
		}			
	}

	

	
	
}
